package io.cici.cc.mybatis.plus.core.injector.methods;

import io.cici.cc.mybatis.lite.mapping.MappedStatement;
import io.cici.cc.mybatis.plus.core.enums.SqlMethod;
import io.cici.cc.mybatis.plus.core.injector.Method;
import io.cici.cc.mybatis.plus.core.metadata.TableInfo;

/**
 * 根据 ID 删除
 */
public class DeleteById extends Method {

    public DeleteById() {
        this(SqlMethod.DELETE_BY_ID.getMethod());
    }

    /**
     * @param name 方法名
     * @since 3.5.0
     */
    public DeleteById(String name) {
        super(name);
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql;
        SqlMethod sqlMethod;
        {
            sqlMethod = SqlMethod.DELETE_BY_ID;
            sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(),
                    tableInfo.getKeyProperty());
            return this.addDeleteMappedStatement(mapperClass, methodName,
                    super.createSqlSource(configuration, sql, Object.class));
        }
    }
}
